/*
 * Copyright (C) 2015 Karumi.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.karumi.dexter.listener.multi;

import com.karumi.dexter.MultiplePermissionsReport;
import com.karumi.dexter.ResourceTable;
import com.karumi.dexter.listener.OnDialogButtonClickListener;
import com.karumi.dexter.uitool.DexterCustomDialog;
import com.karumi.dexter.uitool.DexterLogUtil;
import com.karumi.dexter.uitool.ResTUtil;

import ohos.agp.components.Component;
import ohos.app.Context;

/**
 * Utility listener that shows a {@link DexterCustomDialog} with a minimum configuration when the user rejects
 * any of the requested permissions
 */
public class DialogOnAnyDeniedMultiplePermissionsListener extends BaseMultiplePermissionsListener {
    private final Context context;
    private final String title;
    private final String message;
    private final String positiveButtonText;
    private final String negativeButtonText;
    private final int icon;
    private final OnDialogButtonClickListener onDialogButtonClickListener;

    private DialogOnAnyDeniedMultiplePermissionsListener(
            Context context,
            String title,
            String message,
            String positiveButtonText,
            String negativeButtonText,
            int icon,
            OnDialogButtonClickListener onDialogButtonClickListener) {
        this.context = context;
        this.title = title;
        this.message = message;
        this.positiveButtonText = positiveButtonText;
        this.negativeButtonText = negativeButtonText;
        this.icon = icon;
        this.onDialogButtonClickListener = onDialogButtonClickListener;
    }

    @Override
    public void onPermissionsChecked(MultiplePermissionsReport report) {
        super.onPermissionsChecked(report);
        DexterLogUtil.info("dexterLogUtil", "26-3-1-1");
        if (!report.areAllPermissionsGranted()) {
            DexterLogUtil.info("dexterLogUtil", "26-3-2-1");
            showDialog();
        }
    }

    private void showDialog() {
        new DexterCustomDialog.Builder(context)
                .title(title)
                .content(message)
                .positiveText(
                        positiveButtonText,
                        new DexterCustomDialog.OnPositiveListener() {
                            @Override
                            public void onClick(Component component) {
                                onDialogButtonClickListener.onClick();
                            }
                        })
                .negativeText(negativeButtonText)
                .show();
    }

    /**
     * Builder class to configure the displayed dialog.
     * Non set fields will be initialized to an empty string.
     */
    public static class Builder {
        private final Context context;
        private String title;
        private String message;
        private String buttonText;
        private String negativeButtonText;
        private int icon;
        private OnDialogButtonClickListener onDialogButtonClickListener;

        private Builder(Context context) {
            this.context = context;
        }
        /**
         * 初始化
         *
         * @param context context
         * @return Builder
         */
        public static Builder withContext(Context context) {
            return new Builder(context);
        }

        /**
         * 设置title
         * @param title title名
         * @return Builder
         */
        public Builder withTitle(String title) {
            this.title = title;
            return this;
        }
        /**
         * 设置title
         * @param resId title名
         * @return Builder
         */
        public Builder withTitle(int resId) {
            this.title = ResTUtil.getString(context, resId);
            return this;
        }
        /**
         * 设置message
         * @param message message
         * @return Builder
         */
        public Builder withMessage(String message) {
            this.message = message;
            return this;
        }
        /**
         * 设置message
         * @param resId message
         * @return Builder
         */
        public Builder withMessage(int resId) {
            this.message = ResTUtil.getString(context, resId);
            return this;
        }
        /**
         * 设置 Negative btn text
         * @param id Text
         * @return Builder
         */
        public Builder withNegativeButtonText(int id) {
            this.negativeButtonText = ResTUtil.getString(context, id);
            return this;
        }
        /**
         * 设置 withButtonText
         * @param buttonText Text
         * @return Builder
         */
        public Builder withButtonText(String buttonText) {
            this.buttonText = buttonText;
            return this;
        }
        /**
         * 设置 withButtonText
         * @param resId Text
         * @return Builder
         */
        public Builder withButtonText(int resId) {
            this.buttonText = ResTUtil.getString(context, resId);
            return this;
        }

        /**
         * 设置withButtonText
         * @param buttonText buttonText
         * @param onDialogButtonClickListener onDialogButtonClickListener
         * @return Builder
         */
        public Builder withButtonText(String buttonText, OnDialogButtonClickListener onDialogButtonClickListener) {
            this.buttonText = buttonText;
            this.onDialogButtonClickListener = onDialogButtonClickListener;
            return this;
        }

        /**
         * 设置withButtonText
         * @param resId resId
         * @param onDialogButtonClickListener onDialogButtonClickListener
         * @return Builder
         */
        public Builder withButtonText(int resId, OnDialogButtonClickListener onDialogButtonClickListener) {
            this.buttonText = ResTUtil.getString(context, resId);
            this.onDialogButtonClickListener = onDialogButtonClickListener;
            return this;
        }

        /**
         * 设置icon
         * @param icon icon
         * @return Builder
         */
        public Builder withIcon(int icon) {
            this.icon = icon;
            return this;
        }

        /**
         * 开始builder
         * @return DialogOnAnyDeniedMultiplePermissionsListener
         */
        public DialogOnAnyDeniedMultiplePermissionsListener build() {
            String titleText = this.title == null ? "" : this.title;
            String msg = this.message == null ? "" : this.message;
            String btnText = this.buttonText == null ? "" : this.buttonText;
            String negButtonText = this.negativeButtonText == null ? "" : this.negativeButtonText;
            OnDialogButtonClickListener onDialogBtnClickListener =
                    this.onDialogButtonClickListener != null
                            ? this.onDialogButtonClickListener
                            : new OnDialogButtonClickListener() {
                                @Override
                                public void onClick() {}
                            };
            return new DialogOnAnyDeniedMultiplePermissionsListener(
                    context, titleText, msg, btnText, negButtonText, icon, onDialogBtnClickListener);
        }
    }
}
